package com.wei.czz.framework.admin.helper;

import com.wei.czz.common.enums.CommonEnum;
import com.wei.czz.common.utils.CopyUtils;
import com.wei.czz.framework.admin.entity.RoleEntity;
import com.wei.czz.framework.admin.entity.UserRoleEntity;
import com.wei.czz.framework.admin.service.RoleService;
import com.wei.czz.framework.admin.service.UserRoleService;
import lombok.AllArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.util.Collections;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 *
 * @author wyw
 * date: 2024-06-02 10:36:42
 * className: RoleHelper 角色帮助类
 * version: 1.0
 * description:
 */
@Component
@AllArgsConstructor
public class RoleHelper {

    private static final Logger log = LoggerFactory.getLogger(RoleHelper.class);

    private final RoleService roleService;

    private final UserRoleService userRoleService;

    /**
     * 获取用户关联角色列表操作
     * @param userId 用户主键
     * @return 角色对象列表
     */
    public List<RoleEntity> getRoleList(Long userId) {
        /*
            获取用户关联角色
         */
        List<UserRoleEntity> userRoleList = userRoleService.findList(userId, null, CommonEnum.ZERO.getValue());
        if (userRoleList.isEmpty()) {
            return Collections.emptyList();
        }
        List<Long> roleIdList = CopyUtils.mapList(userRoleList, UserRoleEntity::getRoleId);
        /*
            获取角色
         */
        return roleService.getList(roleIdList);
    }
}
